odoo.define('nd_common.show_price', function (require) {
    'use strict'
    var relational_fields = require('web.relational_fields');
    var FieldOne2Many = relational_fields.FieldOne2Many;
    var FieldMany2One = relational_fields.FieldMany2One;

    FieldMany2One.include({
        _renderEdit: function (event) {
            this._super.apply(this, arguments)
            var self = this;
            var mode = self.mode
            var name = self.name
            var model = self.model
            var m2o_value = self.m2o_value
            if (name === 'product_id' && mode === 'edit' && model === 'sale.order.line' && m2o_value) {
                // event.preventDefault();
                // event.stopPropagation();
                var product = self.record.data.product_id.data['id']
                this._rpc({
                    model: 'sale.order.line',
                    method: 'get_history_price_info',
                    kwargs: {product_id: product, name_value: m2o_value},
                }).then(function (info) {
                    $('.history_price').remove()
                    var str_info = '<p class="history_price">' + info + '</p>'
                    $('[name="order_line"]').append($(str_info))
                });
            }
            ;
        },
    });

    FieldOne2Many.include({
        _onEditLine: function (ev) {
            this._super.apply(this, arguments)
            var index = ev.data.index
            var self = this
            try {
                var sale_line_id = self.record.data.order_line.data[index].res_id
            }catch (e) {
                return
            }
            var mode = self.mode
            var name = self.name
            var model = self.model
            if (name === 'order_line' && mode === 'edit' && model === 'sale.order' && sale_line_id) {
                event.preventDefault();
                event.stopPropagation();
                this._rpc({
                    model: 'sale.order.line',
                    method: 'get_history_price_info',
                    kwargs: {line_id: sale_line_id},
                }).then(function (info) {
                    $('.history_price').remove()
                    var str_info = '<p class="history_price">' + info + '</p>'
                    ev.target.getParent().$el.append($(str_info))
                });
            } else if (name === 'order_line' && mode === 'edit') {
                var index_p = ev.data.index
                var data_p = self.record.data.order_line.data[index_p].data.product_id.data
                if (data_p) {
                    var product_p = data_p.id
                    var name_p = data_p.display_name
                    event.preventDefault();
                    event.stopPropagation();
                    this._rpc({
                        model: 'sale.order.line',
                        method: 'get_history_price_info',
                        kwargs: {product_id: product_p, name_value: name_p},
                    }).then(function (info) {
                        $('.history_price').remove()
                        var str_info = '<p class="history_price">' + info + '</p>'
                        ev.target.getParent().$el.append($(str_info))
                    });
                }
            }
        },
    });

});

